/*
生命周期： 'a
*/
#![allow(unused)]

mod my_module;

use std::fs::File;
use std::io;
use std::io::{ErrorKind, Read};
use std::thread::sleep;
use std::time;
use my_module::my_func1;

fn main() {
    let x = String::from("Hello");
    let y = "xy2a";
    let c = longest(&x, &y);
    println!("{}", c);

    let f = File::open("hello.txt");
    let f = f.unwrap_or_else(|error| match error.kind() {
        ErrorKind::NotFound => match File::create("hello.txt") {
            Ok(fc) => fc,
            Err(e) => panic!("Problem creating the file: {:?}", e),
        },
        _ => panic!("Problem opening the file"),
    });
    println!("{:?}", f);
    read_username_from_file();
    my_func1();
    // 暂停十秒
    sleep(time::Duration::from_secs(10));
}

fn longest<'a>(x: &'a str, y: &'a str) -> &'a str {
    if x.len() > y.len() { x } else { y }
}

fn read_username_from_file() -> Result<String, std::io::Error> {
    let mut f = File::open("hello.txt")?;

    let mut s = String::new();

    f.read_to_string(&mut s)?;
    Ok(s)
}
